package com.ruoyi.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.core.utils.AliyunOssUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.model.LoginInfoVo;
import com.ruoyi.user.utils.DuanxinUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @description: DuanxinController
 * @Author 王辉
 * @Version 1.0
 * @date 2023/1/11 14:31
 */
@RestController
@Slf4j
@RequestMapping("/duanxin")
@CrossOrigin
public class DuanxinController {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation("发送短信")
    @PostMapping("/sendDuanxin")
    public AjaxResult send(@RequestBody(required=false) LoginInfoVo loginInfoVo) {
        //获取四位验证码
        String code = RandomUtil.randomNumbers(4);
        //使用 redis 设置 24小时
        redisTemplate.opsForValue().set(loginInfoVo.getUserMobile(),code,24, TimeUnit.HOURS);
        log.info("验证码为：{}",code);
        //发送短信
        Boolean flag  = DuanxinUtils.sendDuanxin(loginInfoVo.getUserMobile(), code);
        return AjaxResult.success("短信发送成功请注意查收",flag);
    }

    @ApiOperation("身份证上传")
    @PostMapping("/ossUpload")
    public AjaxResult ossUpload(@RequestPart MultipartFile file) {
        AliyunOssUtil oss = AliyunOssUtil.OSS();
        String upload = oss.upload(file);
        return AjaxResult.success(upload);
    }


}
